//
//  AttentionCollectionViewCell.m
//  Arts1
//
//  Created by apple on 15/9/6.
//  Copyright (c) 2015年 Arts1. All rights reserved.
//

#import "AttentionCollectionViewCell.h"
#import "AllHeader.h"


#define kURL @"http://arts1.dlab.com.cn/app/setfollow"
@implementation AttentionCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        
        _backGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 20, 60 * HEIGHT)];
        _backGroundView.layer.cornerRadius = 5;
        _backGroundView.backgroundColor = [UIColor whiteColor];
        [UIView viewShadowColor:_backGroundView];
        [self addSubview:_backGroundView];
        
        
        _imageIcon = [[UIImageView alloc] initWithFrame:CGRectMake(10 * WIDTH, 15 * HEIGHT, 28 * WIDTH, 28 * HEIGHT)];
        _imageIcon.layer.cornerRadius = 14;
        _imageIcon.layer.masksToBounds = YES;
        _imageIcon.backgroundColor = kRandomColor;
        [_backGroundView addSubview:_imageIcon];
        
        
        _userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(_imageIcon.right + 5, 22 * HEIGHT, 120 * WIDTH, 12 * HEIGHT)];
        _userNameLabel.font = [UIFont systemFontOfSize:12];
//        _userNameLabel.text = @"Ra Ra Records";
        [_backGroundView addSubview:_userNameLabel];
        
        
        _attentionBTN = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _attentionBTN.frame = CGRectMake(SCREEN_WIDTH - 100 * RADIO, 17 * RADIO, 70 * RADIO, 28 * RADIO);
        _attentionBTN.layer.cornerRadius = 5;
        _attentionBTN.layer.masksToBounds = YES;
        [_attentionBTN setTitle:@"取消关注" forState:(UIControlStateNormal)];
        [_attentionBTN addTarget:self action:@selector(delFriend:) forControlEvents:(UIControlEventTouchUpInside)];
        _attentionBTN.backgroundColor = [UIColor blackColor];
        _attentionBTN.titleLabel.font = [UIFont systemFontOfSize: 12.0];

        [_backGroundView addSubview:_attentionBTN];
    }
    return self;
}

- (void)setModel:(AttentionFriendModel *)model
{
    _model = model;
    [_imageIcon sd_setImageWithURL:[NSURL URLWithString:_model.userFace] placeholderImage:nil options:SDWebImageRetryFailed | SDWebImageLowPriority ];
    _userNameLabel.text = _model.nickName;
}

//取消关注
- (void)delFriend:(UIButton *)delFriend
{
//    [self requestAddFriend:@"cancel"];
//    _cancelFriend(@"cancel");
    
    NSArray *array = @[@"cancel",_model.userId];
    
    [_attentionBTN setTitle:@"关注" forState:UIControlStateNormal];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    NSNotification *cation = [[NSNotification alloc] initWithName:@"delFriend" object:array userInfo:nil];
    [center postNotification:cation];
}


//添加/取消关注
- (void)requestAddFriend:(NSString *)act
{
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    manage.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"dataForKeyAndUid"];
    NSString *uid = dic[@"data"][@"uid"];
    NSString *key = dic[@"data"][@"key"];
    
    NSDictionary *pardic = nil;
    if (uid && key && _model.userId) {
        pardic = @{@"type":@"user",
                   @"uid":uid,
                   @"key":key,
                   @"act":act,
                   @"userId":_model.userId};
    }
    
    [manage GET:kURL parameters:pardic
        success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];

            NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
            NSNotification *cation = [[NSNotification alloc] initWithName:@"reLoadFriendView" object:nil userInfo:nil];
            [center postNotification:cation];
            
            // 请求成功的时候调用这个block
            NSLog(@"数据请求-------------------%@", dic);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            // 请求失败的时候调用调用这个block
            NSLog(@"请求失败");
        }];
}

@end
